home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 23.zip / BS1 part 23 / C-light d2.adf / Random.c < prev    next >
C/C++ Source or Header  |  1988-08-08  |  1KB  |  49 lines

  1. /*------------------------- r a n d o m . c -------------------------*/
  2. /* This routine generates random numbers that are greater than or    */
  3. /* equal to zero and less than one.  If called with a non-zero value */
  4. /* random(1777) the value is used as a new seed.  If called with a   */
  5. /* value of zero, the random number is generated using the old seed. */
  6. /* If no old seed exists, then 177777 is used as a new seed.         */
  7. /*-------------------------------------------------------------------*/
  8.  
  9. #include "stdio.h"
  10. #include "math.h"
  11.  
  12. float random(setseed)
  13. int setseed;
  14. {
  15.    static int a = 8195;
  16.    static int num;
  17.    float value;
  18.    static int oldseed = 0;
  19.    static int TWO_to_27 = 134217728;   /* 134217728 = 2**27 */
  20.  
  21.    if(setseed == 0)
  22.    {
  23.       if(oldseed == 0)  /* Generate own seed. */
  24.       {
  25.          oldseed = 177777;
  26.          num = (a * oldseed)%TWO_to_27;
  27.          oldseed = num;
  28.          value = ((float)num)/((float)TWO_to_27);
  29.          if(value < 0.)value = (-value);
  30.          return(value);
  31.       } else  /* Use old seed. */
  32.       {
  33.          num = (a * oldseed)%TWO_to_27;
  34.          oldseed = num;
  35.          value = ((float)num)/((float)TWO_to_27);
  36.          if(value < 0.)value = (-value);
  37.          return(value);
  38.      }
  39.    } else    /* set seed if provided with one. */
  40.    {
  41.       oldseed = setseed;
  42.       num = (a * oldseed)%TWO_to_27;
  43.       oldseed = num;
  44.       value = ((float)num)/((float)TWO_to_27);
  45.       if(value < 0.)value = (-value);
  46.       return(value);
  47.    }
  48. }
  49.